<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="http://lh188.xyz/imgFiles/bms-img/zhm.jpg">
      </div>
      <!-- 表单 -->
      <el-form ref="refForm" :model="login_form" :rules="loginRules" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input placeholder="用户名" v-model="login_form.username"  prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input placeholder="密码" type="password" v-model="login_form.password" prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btn">
          <el-button type="primary" @click="signUp">登录</el-button>
          <el-button type="info" @click="refForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 登陆表单的数据绑定对象（参数）
      login_form: {
        username: 'admin',
        password: '123456'
      },
      // 这是表单验证规则对象
      loginRules: {
        // 验证用户名
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    refForm () {
      this.$refs.refForm.resetFields()
    },
    // 登录验证
    signUp () {
      this.$refs.refForm.validate(async valid => {
        if (!valid) return alert('用户名或密码不合法')
        const { data: res } = await this.$http.post('login', this.login_form)
        // await只能用在async（异步）修饰的方法中
        if (res.meta.status !== 200) return this.$msg.error('用户名或密码错误！登陆失败')
        this.$msg.success('尊敬的管理员，欢迎您的登录！')
        /*
        *1. 将登陆成功后服务器生成并返回的token存储到客户端的sessionStorage中
        *   1.1 项目中除了登录接口都需要登录之后才能访问
        *   1.2 token只应该在会话期间生效，所以将其保存在sessionStorage中
        */
        window.sessionStorage.setItem('token', res.data.token)
        // 2.登录成功后跳转到首页，路由地址 ‘/home’
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped="scoped">
  .login_container {
    background-color: #2b4b6b;
    height: 100%;
    background: url(http://lh188.xyz/imgFiles/bms-img/banner.jpg);
    background-size: 100% 100%;
  }

  .login_box {
    width: 450px;
    height: 300px;
    background: white;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 5px;
    background-color: #021C4E;
    opacity: 0.8;
    box-shadow: 0 0 20px rgba(255,100,255,.8);
    transition: all 1.5s;
    &:hover{
      box-shadow: 0 0 80px rgba(255,123,255,.8);
    }
    .avatar_box {
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      box-shadow: 0 0 10px #ddd;
      background: white;
      cursor: pointer;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }

  .btn {
    display: flex;
    justify-content: flex-end;
  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 10px 20px;
    box-sizing: border-box; //防止盒子溢出
  }
  .el-input{
    opacity: 0.9;
  }
  .el-button{
    opacity: 0.9;
  }
</style>
